<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/12/12
 * Time: 14:20
 */
namespace App\Http\Controllers\Admin;

use App\Constant\ErrorConstant;
use App\Service\Admin\RefundService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class RefundController extends BaseController
{
    protected $service;

    public function __construct(Request $request, RefundService $service)
    {
        parent::__construct($request);

        $this->service  =   $service;
    }

    /**
     * 售后订单列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function listRefund()
    {
        $query  =   $this->request->post('query', []);
        $limit  =   $this->request->input('page_size', NULL);
        $page   =   $this->request->input('page', 1);

        [$flag, $result] = $this->service->refundList($query, $limit, $page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_REFUND_LIST_ERROR, $result);
        }
    }

    /**
     * 售后订单详情
     * @return \Illuminate\Http\JsonResponse
     */
    public function detailRefund()
    {
        $validator  =   Validator::make($this->request->post(), [
            'refund_id'  =>  'required|integer|exists:mini_order_refund,id'
        ], [
            'refund_id.required' =>  '售后订单ID不能为空',
            'refund_id.integer'  =>  '售后订单ID必须为整型',
            'refund_id.exists'   =>  '售后订单不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_REFUND_DETAIL_ERROR, $validator->errors()->first());
        }

        $refund_id   =   (int) $this->request->post('refund_id');
        [$flag, $result]    =   $this->service->orderDetail($refund_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_ORDER_DETAIL_ERROR, $result);
        }
    }

}
